package com.iddactylogram.client;

import com.iddactylogram.common.domain.NetworkAddress;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class LocalLoadBalancing implements ILoadBalancing {

    private static final List<NetworkAddress> serverList = new ArrayList<>();

    public LocalLoadBalancing() {
    }

    public LocalLoadBalancing(List<NetworkAddress> serverList) {
        LocalLoadBalancing.serverList.addAll(serverList);
    }

    public void addNetworkAddress(NetworkAddress networkAddress) {
        serverList.add(networkAddress);
    }

    public void addNetworkAddress(String ip, int port) {
        serverList.add(new NetworkAddress(ip, port));
    }

    @Override
    public void loadServerInfo(Consumer<Object> balancingAfter) {

    }

    @Override
    public NetworkAddress chooseServerAddress(String appName) {
        int chooseCode = Math.abs(appName.hashCode()) % serverList.size();
        return serverList.get(chooseCode);
    }
}
